<?php

namespace Budget\BudgetBundle\DateHelper;


class DateHelper
{
    const FORMAT_DB         = 'Y-m-d H:i:s';
    const FORMAT_USER       = 'd.m.Y';
    const FORMAT_YEAR_MONTH = 'Y-m';
    const DAY_START         = '00:00:00';
    const DAY_END           = '24:59:59';
    
    // Convert mm.yyyy to yyyy-mm
    public static function convertToYearMonth($date) 
    {
        $tmp = explode('.', $date);
        return $tmp[1].'-'.$tmp[0];
    }
    
    // Convert dd.mm.yyyy to yyyy-mm-dd
    public static function convertToFormatDb($date) 
    {
        $tmp = explode('.', $date);
        return $tmp[2].'-'.$tmp[1].'-'.$tmp[0];
    }
    
    // Get month preview. Format mm.yyyy example 01.2014 => 12.2013
    public static function getMonthPreview($date) 
    {
        $array = explode('.', $date);
        $month = $array[0];
        $year  = $array[1];
        
        if ($month == 1) {
           $month = 12; 
           $year--;
        } else {
            $month--;
        }
        
        if ($month < 10) {
           $month = '0'.$month; 
        }
        
        return $month.'.'.$year;
    }
    
    // Convert number month (1 - January, 12 - December) to name month
    public static function getMonthName($n) 
    {
        $months   = array();
        $months[] = 'January'; 
        $months[] = 'February '; 
        $months[] = 'March'; 
        $months[] = 'April'; 
        $months[] = 'May'; 
        $months[] = 'June';
        $months[] = 'July';
        $months[] = 'August'; 
        $months[] = 'September'; 
        $months[] = 'October'; 
        $months[] = 'November'; 
        $months[] = 'December'; 
        
        return $months[$n-1];
    }
}

